import urllib2,json,re,codecs,os


def get_comment(comment_path,start_url,count):

 for i in range(count):
     try:
         comment_list=[]
         comment_url=start_url+str(i)
         print comment_url
         response = urllib2.urlopen(comment_url).read()
         json_data=json.loads(response.decode("gbk"))["comments"]
         for item in json_data:
            comment=item["content"]
            comment_list.append(comment)
         write_comment(comment_path,comment_list)
     except:
         continue

def write_comment(comment_path,comment_list):
    fo=codecs.open(comment_path,"a")
    for comment in comment_list:
        comment=comment.encode('utf-8')
        fo.write(comment+"\n")
    fo.close()

def start():
    count=100
    itemList=['1993092','1311928','1292426','2883267','2100472','3082122','1993087','2743359','915143','2883257']
    for id in itemList:
      start_url="https://sclub.jd.com/comment/productPageComments.action?productId=%s&score=0&sortType=5&pageSize=10&page="%(id)
      comment_path="comment_list/%s.txt"%(id)
      if os.path.exists(comment_path):
        os.remove(comment_path)
      get_comment(comment_path,start_url,count)
if __name__ == '__main__':
   start()

